﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.Windows.Data;
using GSSoftSL.Utility;
using GSSoftSL.Web.Models;
using GSSoftSL.Web.Services;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Windows.Data.DomainServices;

namespace GSSoftSL.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class KhoaHocViewModelOld : ViewModelBase
    {
        private GSSoftContext _dc;
        public GSSoftContext DC
        {
            get { return _dc; }
        }

        
        private bool _isBusy = false;
        public bool IsBusy
        {
            get
            {
                return _isBusy;
            }
            set
            {
                if (_isBusy == value)
                {
                    return;
                }
                _isBusy = value;
                RaisePropertyChanged("IsBusy");
            }
        }

        /// <summary>
        /// Initializes a new instance of the KhoaHocViewModel class.
        /// </summary>
        public KhoaHocViewModelOld()
        {
            Init();
        }

        private void Init()
        {
            _dc = new GSSoftContext();//(GSSoftContext) context;
            IsBusy = true;
            _dc.Load(_dc.GetMonsQuery().OrderBy(n => n.ThuTu), LoadBehavior.RefreshCurrent, LoadMon_Completed, null);
        }

        private void LoadMon_Completed(LoadOperation<Mon> loadMon)
        {
            IsBusy = false;
            Mons = loadMon.Entities.ToArray();
        }

        private Mon _currentMon;
        public Mon CurrentMon
        {
            get { return _currentMon; }
            set
            {
                if (_currentMon == value)
                    return;
                _currentMon = value;
                if (_currentMon != null)
                {
                    IsBusy = true;
                    _dc.Load(
                        _dc.GetLopsWithKhoaHocsQuery().Where(n => n.MaMon == _currentMon.MaMon && n.TrangThai == 0).
                            OrderBy(n => n.MaLop), LoadBehavior.RefreshCurrent, LoadLop_Completed, null);
                }
                else
                {
                    Lops = null;
                    KhoaHocs = null;
                    CurrentKhoaHoc = null;
                }
                RaisePropertyChanged("CurrentMon");
                if (_dc.HasChanges)
                    _dc.RejectChanges();
            }
        }
        
        private Mon[] _mons = null;
        public Mon[] Mons
        {
            get { return _mons; }
            set
            {
                if (_mons == value)
                {
                    return;
                }
                _mons = value;
                CurrentMon = (_mons != null && _mons.Length > 0) ? _mons.First() : null;
                RaisePropertyChanged("Mons");
            }
        }

        private void LoadLop_Completed(LoadOperation<Lop> loadLop)
        {
            IsBusy = false;
            Lops = loadLop.Entities.ToArray();
            if (Lops.Length > 0)
                CurrentLop = Lops.First();
        }


        private Lop[] _lops = null;
        public Lop[] Lops
        {
            get
            {
                return _lops;
            }
            set
            {
                if (_lops == value)
                {
                    return;
                }
                _lops = value;
                CurrentLop = (_lops != null && _lops.Length > 0) ? _lops.First() : null;
                RaisePropertyChanged("Lops");
            }
        }

        
        private Lop _currentLop = null;
        public Lop CurrentLop
        {
            get
            {
                return _currentLop;
            }
            set
            {
                if (_currentLop == value)
                {
                    return;
                }
                _currentLop = value;
                RaisePropertyChanged("CurrentLop");
                if (_currentLop != null)
                {
                    IsBusy = true;
                    _dc.Load(
                        _dc.GetKhoaHocsWithLopQuery().Where(n => n.LopId == _currentLop.LopId).OrderByDescending(n => n.STT),
                        LoadBehavior.RefreshCurrent, LoadKhoaHoc_Completed, null);
                }
                else
                {
                    KhoaHocs = null;
                    CurrentKhoaHoc = null;
                }
            }
        }

        private void LoadKhoaHoc_Completed(LoadOperation<KhoaHoc> loadKhoaHoc)
        {
            KhoaHocs = new PagedCollectionView(new EntityList<KhoaHoc>(_dc.KhoaHocs, loadKhoaHoc.Entities));
            IsBusy = false;
        }

        private PagedCollectionView _khoaHocs;
        public PagedCollectionView KhoaHocs
        {
            get { return _khoaHocs; }
            set
            {
                _khoaHocs = value;
                RaisePropertyChanged("KhoaHocs");
            }
        }
        
        private KhoaHoc _currentKhoaHoc = null;
        public KhoaHoc CurrentKhoaHoc
        {
            get
            {
                return _currentKhoaHoc;
            }
            set
            {
                if (_currentKhoaHoc == value)
                {
                    return;
                }
                _currentKhoaHoc = value;
                if (_currentKhoaHoc != null && _currentKhoaHoc.MaKhoaHoc == null)
                { 
                    _currentKhoaHoc.MaKhoaHoc = "";
                }
                RaisePropertyChanged("CurrentKhoaHoc");
            }
        }

        private RelayCommand<object> _saveCommand;
        public RelayCommand<object> SaveCommand
        {
            get
            {
                return _saveCommand
                    ?? (_saveCommand = new RelayCommand<object>(
                                          o =>
                                          {
                                              if (CurrentKhoaHoc == null || !_dc.HasChanges || _dc.IsSubmitting)
                                                  return;
                                              ConfirmWindow.CreateNew("Bạn có muốn lưu?", Save);
                                          },
                                          o => !(CurrentKhoaHoc == null || !_dc.HasChanges || _dc.IsSubmitting)));
            }
        }

        public void Save()
        {
            if (CurrentKhoaHoc.EntityState == EntityState.New)
            {
                IsBusy = true;
                CurrentKhoaHoc.KhoaHocId = Guid.NewGuid();
                CurrentKhoaHoc.STT = 1;
                CurrentKhoaHoc.Lop = CurrentLop;
                //CurrentKhoaHoc.MaKhoaHoc = "001001";
                CurrentKhoaHoc.NgayTao = CurrentKhoaHoc.NgayCapNhat = DateTime.Now;
                CurrentKhoaHoc.NguoiTao = CurrentKhoaHoc.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
            }
            else if (CurrentKhoaHoc.EntityState == EntityState.Modified)
            {
                CurrentLop.KhoaHocs.Add(CurrentKhoaHoc);
                CurrentKhoaHoc.NgayCapNhat = DateTime.Now;
                CurrentKhoaHoc.NguoiCapNhat = UtilityFunction.GetCurrentUserId();
                IsBusy = true;

            }
            _dc.SubmitChanges(Save_Completed, null);
        }

        private void Save_Completed(SubmitOperation obj)
        {
            IsBusy = false;
        }

        private RelayCommand _refreshCommand;
        public RelayCommand RefreshCommand
        {
            get
            {
                return _refreshCommand
                    ?? (_refreshCommand = new RelayCommand(Init));
            }
        }

    }
}